from flask import Flask, Blueprint, render_template, session, redirect, url_for, request, Response,jsonify
# 这里注意要导入models中的models，而不是extends中的models
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import text

from App.models import *
from App.ext import db

accommodationblue = Blueprint('accommodationblue', __name__)

@accommodationblue.route('/viewAccommodation',methods=['GET','POST'])
def viewAccommodation():
    result_dict = []
    sql = "select * from student,accommodation where student.sid = accommodation.sid"
    result = db.session.execute(sql)
    for row in result:
        result_dict.append({
            'sid':row[0],
            'sname':row[1],
            'classid':row[2],
            'did':row[6],
            'bedid':row[7]
        })
    return jsonify(result_dict)

@accommodationblue.route('/viewAccommodationByDid',methods=['GET','POST'])
def viewByDid():
    result_dict = []
    did = request.values.get('did')
    sql = "select * from student,accommodation where student.sid = accommodation.sid and accommodation.did = "+did
    result = db.session.execute(sql)
    for row in result:
        result_dict.append({
            'sid': row[0],
            'sname': row[1],
            'classid': row[2],
            'did': row[6],
            'bedid': row[7]
        })
    return jsonify(result_dict)